
#include "Manga_ui.h"
#include <QDebug>

EventDisplayWidget::EventDisplayWidget(QWidget *parent)
    : QWidget(parent)
{
    setupUi();

}

void EventDisplayWidget::setupUi()
{
    // Main horizontal layout for the entire product item
    QHBoxLayout *mainLayout = new QHBoxLayout(this);
    mainLayout->setSpacing(10);

    // Left-side layout for image and details
    QWidget *leftContainer = new QWidget(this);
    QHBoxLayout *leftMainLayout = new QHBoxLayout(leftContainer);
    leftMainLayout->setContentsMargins(0, 0, 0, 0);

    // Image setup on the far left
    imageLabel = new QLabel(this);
    imageLabel->setFixedSize(250, 280);
    leftMainLayout->addWidget(imageLabel);

    // Right of the image: themeLabel, descriptionLabel, dateLabel
    QVBoxLayout *detailsLayout = new QVBoxLayout();
    themeLabel = new QLabel(this);
    themeLabel->setStyleSheet("font-size: 18px; font-weight: bold;");
    detailsLayout->addWidget(themeLabel);

    descriptionLabel = new QLabel(this);
    descriptionLabel->setWordWrap(true);
    detailsLayout->addWidget(descriptionLabel);

    dateLabel = new QLabel(this);
    detailsLayout->addWidget(dateLabel);

    detailsLayout->setSpacing(5);  // Space between theme, description, and date
    leftMainLayout->addLayout(detailsLayout);

    // Add the combined left container to the main layout
    mainLayout->addWidget(leftContainer, 2);  // Stretch factor for left side

    // Right-side layout for price and location
    QVBoxLayout *rightLayout = new QVBoxLayout();
    priceLabel = new QLabel(this);
    priceLabel->setStyleSheet("color: red; font-size: 16px;");
    rightLayout->addWidget(priceLabel);

    locationLabel = new QLabel(this);
    rightLayout->addWidget(locationLabel);
    rightLayout->setSpacing(5);

    mainLayout->addLayout(rightLayout, 1);  // Stretch factor for right side

    // Add a line separator below the product item
    QFrame *separatorLine = new QFrame(this);
    separatorLine->setFrameShape(QFrame::HLine);
    separatorLine->setFrameShadow(QFrame::Sunken);
    separatorLine->setFixedWidth(1620);

    // Outer layout to include the main content and separator
    QVBoxLayout *outerLayout = new QVBoxLayout(this);
    outerLayout->addLayout(mainLayout);
    outerLayout->addWidget(separatorLine);
    outerLayout->setContentsMargins(0, 0, 0, 0);
    outerLayout->setSpacing(10);

    setLayout(outerLayout);

    // Set size constraints
    setFixedHeight(360);  // Fixed height for each item
    setMouseTracking(true);
}
void EventDisplayWidget::setEvent(const QPixmap &image, const QString &theme, const QDate &date,
                                  const QString &location, const QString &price, const QString &description,int productId)
{
    this->productId = productId;
    imageLabel->setFixedSize(250, 280);

    QPixmap scaledImage = image.scaled(imageLabel->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
    imageLabel->setPixmap(scaledImage);
    themeLabel->setText(theme);
    dateLabel->setText("展会日期："+date.toString("MMMM dd, yyyy"));
    locationLabel->setText(location);
    priceLabel->setText("售价："+price);
    descriptionLabel->setText("展会地点："+description); // Set the description text
}

void EventDisplayWidget::onBuyButtonClicked()
{
    emit buyButtonClicked(themeLabel->text(), priceLabel->text(), productId);
}
void EventDisplayWidget::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        emit productClicked(imageLabel->pixmap().copy(), themeLabel->text(),
                            descriptionLabel->text(), locationLabel->text(),dateLabel->text(),
                            priceLabel->text(), productId);
    }
}


EventDisplayWidget::~EventDisplayWidget() {}
